package com.jerry.huihome.module.find.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.jerry.huihome.R;
import com.jerry.huihome.base.BaseApplication;
import com.jerry.huihome.base.BaseViewHolder;
import com.jerry.huihome.base.MyBaseAdapter;
import com.jerry.huihome.module.find.activity.DesignerDetailActivity;
import com.jerry.huihome.module.find.entity.Designer;
import com.jerry.huihome.views.RoundImageView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.squareup.picasso.Picasso;

import java.util.List;

import butterknife.BindView;

/**
 * Created by Administrator on 2016/5/10.
 */
public class DesignerAdapter extends MyBaseAdapter<Designer.BodyBean.DesignersBean> {

    private Context context;
    private List<Designer.BodyBean.DesignersBean> list;
    private LinearLayout.LayoutParams layoutParams;
    private int screenWidth;
    private DisplayImageOptions options;
    private Designer.BodyBean.DesignersBean designersBean;

    public DesignerAdapter(Context context, List<Designer.BodyBean.DesignersBean> list) {
        super(context, list);
        this.context = context;
        this.list = list;
        DisplayMetrics metrics = new DisplayMetrics();
        WindowManager windowManager =
                (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        windowManager.getDefaultDisplay().getMetrics(metrics);
        // 屏幕宽度
        screenWidth =
                metrics.widthPixels;
        layoutParams = new LinearLayout.LayoutParams((int) (screenWidth /4.4),260);
        options = new DisplayImageOptions.Builder().cacheInMemory(true)
                .cacheOnDisk(true)
                .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
                .bitmapConfig(Bitmap.Config.ARGB_8888)
                .build();
    }


    @Override
    public int getLayoutId() {
        return R.layout.designers_list_item;
    }

    @Override
    public BaseViewHolder getViewHolder() {

        return new ViewHolder();
    }

    public List<Designer.BodyBean.DesignersBean> getList(){
        return list;
    }
    @Override
    public void bindViews(View view, BaseViewHolder viewHolder) {
        designersBean = getItem(viewHolder.position);
        ViewHolder myViewHolder = (ViewHolder) viewHolder;
        myViewHolder.tvDesignersName.setText(designersBean.getRealname());
        myViewHolder.ratingBar.setRating(designersBean.getTotalScore());
        myViewHolder.successNum.setText(designersBean.getCaseCount()+"个");
        myViewHolder.designMaxFee.setText(designersBean.getFeeHigh()+"元/㎡");
        myViewHolder.designMinFee.setText(designersBean.getFeeLow()+"—");
//        Picasso.with(context).load(designersBean.getSAvatar()).into(((ViewHolder) viewHolder).ivDesignersHeadImg);
        BaseApplication.getApplication().getImageLoader().displayImage(designersBean.getSAvatar(),myViewHolder.ivDesignersHeadImg,options);
       /* myViewHolder.rivHeadBg.setVisibility(View.VISIBLE);
        myViewHolder.rivHeadBg.setImageResource(R.drawable.ui_icon);*/
        int authFlag = designersBean.getAuthFlag();
        if (authFlag == 0){
            myViewHolder.designAttest.setText("已认证");
        }else {
            myViewHolder.designAttest.setText("未认证");
        }
        int warrantFlag = designersBean.getWarrantFlag();
        if (warrantFlag == 0){
            myViewHolder.designVouch.setText("已担保");
        }else {
            myViewHolder.designVouch.setText("未担保");
        }
        List<Designer.BodyBean.DesignersBean.AbbrWorksBean> abbrWorks = designersBean.getAbbrWorks();
        ImageView imageView = null;
        for (int i = 0; i < abbrWorks.size() ; i++) {

            layoutParams.leftMargin = 20;
            layoutParams.topMargin = 20;
            layoutParams.bottomMargin = 20;
            if (i == abbrWorks.size() - 1)
            {
                layoutParams.rightMargin = 10;
            }

            imageView = new ImageView(context);
            imageView.setLayoutParams(layoutParams);

            Picasso.with(context)
                    .load(abbrWorks.get(i).getImgUrl()).fit()
                    .into(imageView);

            myViewHolder.llWorks.addView(imageView);
            myViewHolder.ivDesignersHeadImg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, DesignerDetailActivity.class);
                    int designerId = designersBean.getUid();
                    intent.putExtra("designerId",designerId);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(intent);
                }
            });
        }
    }

    class ViewHolder extends BaseViewHolder{
        @BindView(R.id.iv_designers_head_img)
        RoundImageView ivDesignersHeadImg;
       /* @BindView(R.id.riv_head_bg)
        ImageView rivHeadBg;*/
        @BindView(R.id.tv_designers_name)
        TextView tvDesignersName;
        @BindView(R.id.ratingBar)
        RatingBar ratingBar;
        @BindView(R.id.success_num)
        TextView successNum;
        @BindView(R.id.design_max_fee)
        TextView designMaxFee;
        @BindView(R.id.design_min_fee)
        TextView designMinFee;
        @BindView(R.id.ll_works)
        LinearLayout llWorks;
        @BindView(R.id.tv_designer_vouch)
        TextView designVouch;
        @BindView(R.id.tv_designer_attest)
        TextView designAttest;
    }
}
